﻿using System.ComponentModel;
using System.Threading;

namespace System
{
    public sealed class AsyncFunc<TResult>
    {
        #region Fields

        private readonly Func<Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<TResult> func)
            : this((isCancelled, reportProgress) => func(), false, false)
        {
        }

        public AsyncFunc(Func<Func<bool>, TResult> func)
            : this((isCancelled, reportProgress) => func(isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<Action<int>, TResult> func)
            : this((isCancelled, reportProgress) => func(reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync()
        {
            return InvokeAsyncCore(false);
        }

        public void InvokeAsync()
        {
            InvokeAsyncCore(false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<AsyncOperation>(Worker);
                worker.BeginInvoke(asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, TResult>
    {
        #region Fields

        private readonly Func<T1, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, TResult> func)
            : this((param1, isCancelled, reportProgress) => func(param1), false, false)
        {
        }

        public AsyncFunc(Func<T1, Func<bool>, TResult> func)
            : this((param1, isCancelled, reportProgress) => func(param1, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, Action<int>, TResult> func)
            : this((param1, isCancelled, reportProgress) => func(param1, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1)
        {
            return InvokeAsyncCore(param1, false);
        }

        public void InvokeAsync(T1 param1)
        {
            InvokeAsyncCore(param1, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, TResult> func)
            : this((param1, param2, isCancelled, reportProgress) => func(param1, param2), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, Func<bool>, TResult> func)
            : this((param1, param2, isCancelled, reportProgress) => func(param1, param2, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, Action<int>, TResult> func)
            : this((param1, param2, isCancelled, reportProgress) => func(param1, param2, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2)
        {
            return InvokeAsyncCore(param1, param2, false);
        }

        public void InvokeAsync(T1 param1, T2 param2)
        {
            InvokeAsyncCore(param1, param2, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, TResult> func)
            : this((param1, param2, param3, isCancelled, reportProgress) => func(param1, param2, param3), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, Func<bool>, TResult> func)
            : this((param1, param2, param3, isCancelled, reportProgress) => func(param1, param2, param3, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, Action<int>, TResult> func)
            : this((param1, param2, param3, isCancelled, reportProgress) => func(param1, param2, param3, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3)
        {
            return InvokeAsyncCore(param1, param2, param3, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3)
        {
            InvokeAsyncCore(param1, param2, param3, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, TResult> func)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => func(param1, param2, param3, param4), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => func(param1, param2, param3, param4, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => func(param1, param2, param3, param4, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            InvokeAsyncCore(param1, param2, param3, param4, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, TResult> func)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, T7, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, T7, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, T7, T8, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, T7, T8, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFunc<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14), false, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled), true, false)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Action<int>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, reportProgress), false, true)
        {
        }

        public AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFunc{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFunc(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<TProgress, TResult>
    {
        #region Fields

        private readonly Func<Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<TResult> func)
            : this((isCancelled, reportProgress) => func(), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<Func<bool>, TResult> func)
            : this((isCancelled, reportProgress) => func(isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<Action<int, TProgress>, TResult> func)
            : this((isCancelled, reportProgress) => func(reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync()
        {
            return InvokeAsyncCore(false);
        }

        public void InvokeAsync()
        {
            InvokeAsyncCore(false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<AsyncOperation>(Worker);
                worker.BeginInvoke(asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, TResult> func)
            : this((param1, isCancelled, reportProgress) => func(param1), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, Func<bool>, TResult> func)
            : this((param1, isCancelled, reportProgress) => func(param1, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, Action<int, TProgress>, TResult> func)
            : this((param1, isCancelled, reportProgress) => func(param1, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1)
        {
            return InvokeAsyncCore(param1, false);
        }

        public void InvokeAsync(T1 param1)
        {
            InvokeAsyncCore(param1, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, TResult> func)
            : this((param1, param2, isCancelled, reportProgress) => func(param1, param2), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, Func<bool>, TResult> func)
            : this((param1, param2, isCancelled, reportProgress) => func(param1, param2, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, Action<int, TProgress>, TResult> func)
            : this((param1, param2, isCancelled, reportProgress) => func(param1, param2, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2)
        {
            return InvokeAsyncCore(param1, param2, false);
        }

        public void InvokeAsync(T1 param1, T2 param2)
        {
            InvokeAsyncCore(param1, param2, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, TResult> func)
            : this((param1, param2, param3, isCancelled, reportProgress) => func(param1, param2, param3), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, Func<bool>, TResult> func)
            : this((param1, param2, param3, isCancelled, reportProgress) => func(param1, param2, param3, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, isCancelled, reportProgress) => func(param1, param2, param3, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3)
        {
            return InvokeAsyncCore(param1, param2, param3, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3)
        {
            InvokeAsyncCore(param1, param2, param3, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, TResult> func)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => func(param1, param2, param3, param4), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => func(param1, param2, param3, param4, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => func(param1, param2, param3, param4, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            InvokeAsyncCore(param1, param2, param3, param4, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, TResult> func)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, T7, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, T7, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncFuncWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TProgress, TResult>
    {
        #region Fields

        private readonly Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int, TProgress>, TResult> _func;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14), false, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled), true, false)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Action<int, TProgress>, TResult> func)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, reportProgress), false, true)
        {
        }

        public AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int, TProgress>, TResult> func)
            : this(func, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncFuncWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TProgress, TResult}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncFuncWithProgress(Func<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int, TProgress>, TResult> func, bool isCancellable, bool canReportProgress)
        {
            _func = func;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncFuncCompletedEventHandler<TResult> Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncFuncCompletedEventArgs<TResult>)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14, AsyncOperation asyncOp)
        {
            var result = default(TResult);
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    result = _func(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncFuncCompletedEventArgs<TResult>(result, error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction
    {
        #region Fields

        private readonly Action<Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action action)
            : this((isCancelled, reportProgress) => action(), false, false)
        {
        }

        public AsyncAction(Action<Func<bool>> action)
            : this((isCancelled, reportProgress) => action(isCancelled), true, false)
        {
        }

        public AsyncAction(Action<Action<int>> action)
            : this((isCancelled, reportProgress) => action(reportProgress), false, true)
        {
        }

        public AsyncAction(Action<Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync()
        {
            return InvokeAsyncCore(false);
        }

        public void InvokeAsync()
        {
            InvokeAsyncCore(false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<AsyncOperation>(Worker);
                worker.BeginInvoke(asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1>
    {
        #region Fields

        private readonly Action<T1, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1> action)
            : this((param1, isCancelled, reportProgress) => action(param1), false, false)
        {
        }

        public AsyncAction(Action<T1, Func<bool>> action)
            : this((param1, isCancelled, reportProgress) => action(param1, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, Action<int>> action)
            : this((param1, isCancelled, reportProgress) => action(param1, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1)
        {
            return InvokeAsyncCore(param1, false);
        }

        public void InvokeAsync(T1 param1)
        {
            InvokeAsyncCore(param1, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2>
    {
        #region Fields

        private readonly Action<T1, T2, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2> action)
            : this((param1, param2, isCancelled, reportProgress) => action(param1, param2), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, Func<bool>> action)
            : this((param1, param2, isCancelled, reportProgress) => action(param1, param2, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, Action<int>> action)
            : this((param1, param2, isCancelled, reportProgress) => action(param1, param2, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2)
        {
            return InvokeAsyncCore(param1, param2, false);
        }

        public void InvokeAsync(T1 param1, T2 param2)
        {
            InvokeAsyncCore(param1, param2, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3>
    {
        #region Fields

        private readonly Action<T1, T2, T3, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3> action)
            : this((param1, param2, param3, isCancelled, reportProgress) => action(param1, param2, param3), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, Func<bool>> action)
            : this((param1, param2, param3, isCancelled, reportProgress) => action(param1, param2, param3, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, Action<int>> action)
            : this((param1, param2, param3, isCancelled, reportProgress) => action(param1, param2, param3, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3)
        {
            return InvokeAsyncCore(param1, param2, param3, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3)
        {
            InvokeAsyncCore(param1, param2, param3, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4> action)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => action(param1, param2, param3, param4), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, Func<bool>> action)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => action(param1, param2, param3, param4, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, Action<int>> action)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => action(param1, param2, param3, param4, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            InvokeAsyncCore(param1, param2, param3, param4, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5> action)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, Action<int>> action)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6> action)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6, T7>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7> action)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6, T7}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6, T7, T8>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6, T7, T8}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6, T7, T8, T9>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6, T7, T8, T9}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncAction<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14), false, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled), true, false)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Action<int>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, reportProgress), false, true)
        {
        }

        public AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncAction{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncAction(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp)
        {
            var e = new ProgressChangedEventArgs(progressPercentage, asyncOp.UserSuppliedState);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? p => ReportProgressChanged(p, asyncOp)
                        : default(Action<int>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<TProgress>
    {
        #region Fields

        private readonly Action<Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action action)
            : this((isCancelled, reportProgress) => action(), false, false)
        {
        }

        public AsyncActionWithProgress(Action<Func<bool>> action)
            : this((isCancelled, reportProgress) => action(isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<Action<int, TProgress>> action)
            : this((isCancelled, reportProgress) => action(reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync()
        {
            return InvokeAsyncCore(false);
        }

        public void InvokeAsync()
        {
            InvokeAsyncCore(false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<AsyncOperation>(Worker);
                worker.BeginInvoke(asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, TProgress>
    {
        #region Fields

        private readonly Action<T1, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1> action)
            : this((param1, isCancelled, reportProgress) => action(param1), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, Func<bool>> action)
            : this((param1, isCancelled, reportProgress) => action(param1, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, Action<int, TProgress>> action)
            : this((param1, isCancelled, reportProgress) => action(param1, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1)
        {
            return InvokeAsyncCore(param1, false);
        }

        public void InvokeAsync(T1 param1)
        {
            InvokeAsyncCore(param1, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2> action)
            : this((param1, param2, isCancelled, reportProgress) => action(param1, param2), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, Func<bool>> action)
            : this((param1, param2, isCancelled, reportProgress) => action(param1, param2, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, Action<int, TProgress>> action)
            : this((param1, param2, isCancelled, reportProgress) => action(param1, param2, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2)
        {
            return InvokeAsyncCore(param1, param2, false);
        }

        public void InvokeAsync(T1 param1, T2 param2)
        {
            InvokeAsyncCore(param1, param2, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3> action)
            : this((param1, param2, param3, isCancelled, reportProgress) => action(param1, param2, param3), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, Func<bool>> action)
            : this((param1, param2, param3, isCancelled, reportProgress) => action(param1, param2, param3, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, Action<int, TProgress>> action)
            : this((param1, param2, param3, isCancelled, reportProgress) => action(param1, param2, param3, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3)
        {
            return InvokeAsyncCore(param1, param2, param3, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3)
        {
            InvokeAsyncCore(param1, param2, param3, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4> action)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => action(param1, param2, param3, param4), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, Func<bool>> action)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => action(param1, param2, param3, param4, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, isCancelled, reportProgress) => action(param1, param2, param3, param4, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4)
        {
            InvokeAsyncCore(param1, param2, param3, param4, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5> action)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6> action)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, T7, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7> action)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, T7, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

    public sealed class AsyncActionWithProgress<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TProgress>
    {
        #region Fields

        private readonly Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int, TProgress>> _action;
        private readonly bool _isCancellable;
        private readonly bool _canReportProgress;
        private bool _isCancelled;
        private int _isBusy;

        #endregion

        #region Constructors

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14), false, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled), true, false)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Action<int, TProgress>> action)
            : this((param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress) => action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, reportProgress), false, true)
        {
        }

        public AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int, TProgress>> action)
            : this(action, true, true)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="AsyncActionWithProgress{T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, TProgress}"/> class.
        /// </summary>
        /// <remarks>
        /// This constructor is private and it is used by all the public constructors to infer
        /// capabilities from the signature used.
        /// </remarks>
        private AsyncActionWithProgress(Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, Func<bool>, Action<int, TProgress>> action, bool isCancellable, bool canReportProgress)
        {
            _action = action;
            _isCancellable = isCancellable;
            _canReportProgress = canReportProgress;
            _isBusy = 0;
            _isCancelled = false;
        }

        #endregion

        #region Events

        public event AsyncCompletedEventHandler Completed;

        public event ProgressChangedEventHandler<TProgress> ProgressChanged;

        #endregion

        #region Properties

        public object Sender { get; set; }

        public bool IsBusy
        {
            get
            {
                return _isBusy > 0;
            }
        }

        #endregion

        #region Methods

        public bool TryInvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14)
        {
            return InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, false);
        }

        public void InvokeAsync(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14)
        {
            InvokeAsyncCore(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, false);
        }

        public void Cancel()
        {
            if (!_isCancellable)
            {
                throw new InvalidOperationException("Delegate supplied in constructor doesn't handle cancelation");
            }

            _isCancelled = true;
        }

        private bool InvokeAsyncCore(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14, bool throwIfFailed)
        {
            var success = Interlocked.CompareExchange(ref _isBusy, 1, 0) == 0;
            if (success)
            {
                _isCancelled = false;
                var asyncOp = AsyncOperationManager.CreateOperation(null);
                var worker = new Action<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12, T13, T14, AsyncOperation>(Worker);
                worker.BeginInvoke(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, asyncOp, null, null);
            }
            else
            {
                if (throwIfFailed)
                {
                    throw new InvalidOperationException("Operation is stil executing");
                }
            }

            return success;
        }

        private void CompletedCallback(object operationState)
        {
            var e = (AsyncCompletedEventArgs)operationState;
            var snapshot = Completed;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressCallback(object operationState)
        {
            var e = (ProgressChangedEventArgs<TProgress>)operationState;
            var snapshot = ProgressChanged;
            if (snapshot != null)
            {
                snapshot(Sender, e);
            }
        }

        private void ReportProgressChanged(int progressPercentage, AsyncOperation asyncOp, TProgress data)
        {
            var e = new ProgressChangedEventArgs<TProgress>(progressPercentage, asyncOp.UserSuppliedState, data);
            asyncOp.Post(ReportProgressCallback, e);
        }

        private void Worker(T1 param1, T2 param2, T3 param3, T4 param4, T5 param5, T6 param6, T7 param7, T8 param8, T9 param9, T10 param10, T11 param11, T12 param12, T13 param13, T14 param14, AsyncOperation asyncOp)
        {
            var error = default(Exception);

            try
            {
                if (!_isCancelled)
                {
                    // Prepare callback delegates
                    var isCancelled = _isCancellable
                        ? () => _isCancelled
                        : default(Func<bool>);
                    var reportProgress = _canReportProgress
                        ? (p, data) => ReportProgressChanged(p, asyncOp, data)
                        : default(Action<int, TProgress>);

                    // Invoke delegate synchronously
                    _action(param1, param2, param3, param4, param5, param6, param7, param8, param9, param10, param11, param12, param13, param14, isCancelled, reportProgress);
                }
            }
            catch (Exception ex)
            {
                error = ex;
            }
            finally
            {
                _isBusy = 0;
                var e = new AsyncCompletedEventArgs(error, _isCancelled, null);
                asyncOp.PostOperationCompleted(CompletedCallback, e);
            }
        }

        #endregion
    }

}
